﻿//-----------------------------------------------------------------------------
// Author: JCBDigger
// URL: http://Games.DiscoverThat.co.uk
//-----------------------------------------------------------------------------
// Control for selecting a colour and converting to XNA colour format
//-----------------------------------------------------------------------------

#region MIT License
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Xna.Framework;


namespace Engine.FormControls
{
    /// <summary>
    /// Has a button to change the colour value and a numeric field for the alpha channel.
    /// </summary>
    public partial class ColourSelectButtonControl : UserControl
    {
        /// <summary>
        /// Raised when the colour or alpha is changed.
        /// </summary>
        public event EventHandler<EventArgs> ColourChanged;

        private static Vector4 customColour1 = new Vector4(0.25f, 0.25f, 0.17f, 1f);
        private static Vector4 customColour2 = new Vector4(0.734375f, 0.7109375f, 0.6171875f, 1f);
        private static Vector4 customColour3 = new Vector4(0.2539063f, 0.2890625f, 0.3125f, 1f);

        public ColourSelectButtonControl()
        {
            InitializeComponent();
            numericAlpha.ValueChanged += new EventHandler(numericAlpha_ValueChanged);
        }

        private void numericAlpha_ValueChanged(object sender, EventArgs e)
        {
            RaiseChangedEvent();
        }

        private void RaiseChangedEvent()
        {
            if (ColourChanged != null)
            {
                ColourChanged(this, EventArgs.Empty);
            }
        }

        //////////////////////////////////////////////////////////////////////
        //
        #region Properties
        //
        /// <summary>
        /// XNA framework colour for the button background.
        /// Includes the alpha.
        /// </summary>
        public Microsoft.Xna.Framework.Color Colour
        {
            get
            {
                return ARGBToXNA((int)numericAlpha.Value, buttonColour.BackColor.R, buttonColour.BackColor.G, buttonColour.BackColor.B);
            }
            set
            {
                numericAlpha.Value = (decimal)value.A;
                buttonColour.BackColor = ARGBToColor(255, value.R, value.G, value.B);
            }
        }
        /// <summary>
        /// The label to display on the button.
        /// </summary>
        public string TextLegend
        {
            get { return buttonColour.Text; }
            set 
            {
                buttonColour.Text = value; 
            }
        }
        /// <summary>
        /// Display or hide the numeric field for entering the alpha value.
        /// Alpha is often used for transparency.
        /// </summary>
        public bool ShowAlpha
        {
            get { return numericAlpha.Visible; }
            set
            {
                if (value)
                {
                    Size size = MinimumSize;
                    size.Width = 118;
                    MinimumSize = size;
                    Size = size;
                }
                else
                {
                    Size size = MinimumSize;
                    size.Width = 75;
                    MinimumSize = size;
                    Size = size;
                    numericAlpha.Value = (decimal)255;
                }
                numericAlpha.Visible = value;
            }
        }
        #endregion
        //
        //////////////////////////////////////////////////////////////////////


        //////////////////////////////////////////////////////////////////////
        //
        #region Helpers
        //
        /// <summary>
        /// Convert an XNA framework colour to a Windows system colour.
        /// </summary>
        private System.Drawing.Color XNAToColor(Microsoft.Xna.Framework.Color colour)
        {
            return VectorToColor(colour.ToVector4());
        }
        /// <summary>
        /// Convert a Windows system colour to an XNA framework colour.
        /// </summary>
        private Microsoft.Xna.Framework.Color ColorToXNA(System.Drawing.Color colour)
        {
            return new Microsoft.Xna.Framework.Color(ColorToVector(colour));
        }
        /// <summary>
        /// Create from individual bytes.
        /// </summary>
        private Microsoft.Xna.Framework.Color ARGBToXNA(int A, int R, int G, int B)
        {
            return new Microsoft.Xna.Framework.Color(R, G, B, A);
        }
        /// <summary>
        /// Convert an XNA lighting colour vector to a Windows system colour.
        /// </summary>
        private System.Drawing.Color VectorToColor(Vector4 colour)
        {
            return System.Drawing.Color.FromArgb((int)(colour.W * 255f), (int)(colour.X * 255f), (int)(colour.Y * 255f), (int)(colour.Z * 255f));
        }
        /// <summary>
        /// Create from individual bytes.
        /// </summary>
        private System.Drawing.Color ARGBToColor(int A, int R, int G, int B)
        {
            return System.Drawing.Color.FromArgb(A, R, G, B);
        }
        /// <summary>
        /// Convert a Windows system colour to an XNA lighting colour vector.
        /// </summary>
        private Vector4 ColorToVector(System.Drawing.Color colour)
        {
            return new Vector4((float)colour.R / 255f, (float)colour.G / 255f, (float)colour.B / 255f, (float)colour.A / 255f);
        }
        /// <summary>
        /// Convert to the int32 colour values for use with the colorDialog custom colours. 
        /// </summary>
        private int ColorToBGR(System.Drawing.Color colour)
        {
            // All three of these produce the same results
            //return (colour.B << 16) + (colour.G << 8) + colour.R;
            //return colour.R + (colour.G * 256) + (colour.B * 256 * 256);
            return System.Drawing.ColorTranslator.ToWin32(colour);
        }
        #endregion
        //
        /////////////////////////////////////////////////////////////////////

        //////////////////////////////////////////////////////////////////////
        //
        #region Change
        //
        /// <summary>
        /// Display the colour selection dialogue.
        /// </summary>
        private void ChangeColourDialogue()
        {
            ColorDialog colourDialog = new ColorDialog();
            colourDialog.Color = buttonColour.BackColor;
            colourDialog.AnyColor = true;
            colourDialog.AllowFullOpen = true;
            colourDialog.FullOpen = true;
            colourDialog.SolidColorOnly = true;
            colourDialog.CustomColors = CustomColours();
            if (colourDialog.ShowDialog() == DialogResult.OK)
            {
                buttonColour.BackColor = colourDialog.Color;
                RaiseChangedEvent();
            }
        }
        /// <summary>
        /// A set of custom colurs used for the colour dialog.
        /// </summary>
        private int[] CustomColours()
        {
            return new int[3] { 
                ColorToBGR(VectorToColor(customColour1)), ColorToBGR(VectorToColor(customColour2)), 
                ColorToBGR(VectorToColor(customColour3)) };
        }

        private void buttonColour_Click(object sender, EventArgs e)
        {
            ChangeColourDialogue();
        }
        #endregion
        //
        //////////////////////////////////////////////////////////////////////

    }
}
